//
//  LXCarSearchViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/1/15.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXCarSearchViewController.h"
#import "CheLiangListCell.h"
#import "CheLiangDetailViewController.h"
#import "LXCarLeaseApplyViewController.h"
#import "LXCarLeaseApplyViewController.h"


@interface LXCarSearchViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@property (nonatomic) LXTopSearchView *searchView;
@property (nonatomic) NSString *searchText;

@end

@implementation LXCarSearchViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    

    [self layoutCustomNav:self.view title:@"搜索"];
    if (kUserDefaultForBool(kRefreshStr)) {
        [self.tableView.mj_header beginRefreshing];
    }
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    LXTopSearchView *searchView = [[LXTopSearchView alloc] initWithFrame:CGRectMake(0,LL_StatusBarAndNavigationBarHeight, kScreenWidth, 70)];
    searchView.textField.placeholder = !StringIsEmpty(self.placeholder)?self.placeholder:@"输入车牌号查询";
    [searchView.textField becomeFirstResponder];
    [self.view addSubview:searchView];
    self.searchView = searchView;
    // 点击
    [searchView setClickBtnBlock:^(NSString * _Nonnull text) {
        [self.view endEditing:YES];
        
        if (!StringIsEmpty(text)) {
            self.searchText = text;
            [self.tableView.mj_header beginRefreshing];
        }
        else {
            
            showAlert(@"请输入关键字搜索");
            
            [self.data removeAllObjects];
            [self.tableView reloadData];
            [self isHaveEmptyData];
        }
        
        
    }];
    
    
    
    [self.view addSubview:self.tableView];
        
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
    }];

    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        [self loadData];
    }];
    
    [self isHaveEmptyData];
    
}
- (void)loadData{
        
    
    NSMutableDictionary *dic = @{
        @"page":@(self.pageIndex),
        @"rows":@(10),
        @"signType":@(2), // 全部
    }.mutableCopy;
    

    if (!StringIsEmpty(self.searchText)) {
        [dic setValue:self.searchText forKey:@"carNum"];
    }
    [MSNetworkManager postRevertUrl:CarList_URL params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        if (isSuccess) {
            
            if ([self.tableView.mj_header isRefreshing]||self.pageIndex==1) {
                [self.data removeAllObjects];
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
            NSInteger totalPage = [resonseObj[@"total"] intValue];
            
            NSArray *cacheArray = resonseObj[@"list"];
            [self.data addObjectsFromArray:[CheLiangModel mj_objectArrayWithKeyValuesArray:cacheArray]];
            
            if (self.data.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            [self isHaveEmptyData];
            [self.tableView reloadData];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
    } withShowHud:YES];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CheLiangListCell *cell = [tableView dequeueReusableCellWithIdentifier:[CheLiangListCell reuseIdentifier]];
    if (!cell) {
        cell = [[CheLiangListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CheLiangListCell reuseIdentifier]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CheLiangModel *model = self.data[indexPath.row];
    cell.allModel = model;
    
   
    
    
    
//    [cell.cancelBtn whenTapped:^{
//        // 未出租
//        if ([model.hireFlag intValue]==0) {
//            LXCarLeaseApplyViewController *vc = [[LXCarLeaseApplyViewController alloc] init];
//            vc.carModel = model;
//            vc.type = 1;
//            [self pushToNextController:vc];
//        }
//        // 出租中
//        else if([model.hireFlag intValue]==1){
//
//            NSMutableDictionary *params = [NSMutableDictionary dictionary];
//            [params setObject:model.ID forKey:@"id"];
//            [params setObject:@"1" forKey:@"type"];
//
//            [MSNetworkManager postRevertUrl:DownUp_URL params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
//                if (isSuccess) {
//                    showAlert(@"下架成功");
//                    [self.tableView.mj_header beginRefreshing];
//                }
//
//            } failure:^(NSUInteger code, NSString * _Nonnull message) {
//            } withShowHud:YES];
//
//
//        }
//
//
//
//    }];
    
    return cell;
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CheLiangModel  *model = self.data[indexPath.row];
    CheLiangDetailViewController *vc = [[CheLiangDetailViewController alloc] init];
//    vc.type = 1;
    vc.detailModel = model;
    [self pushToNextController:vc];
    
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, MaxY(self.searchView), kScreenWidth, kScreenHeight - MaxY(self.searchView)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 108;
        _tableView.backgroundColor = ColorWithF5F5F5;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerNib:[UINib nibWithNibName:[CheLiangListCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[CheLiangListCell reuseIdentifier]];

    }
    return _tableView;
}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}
@end
